package org.letva.toink.blog.wicket;

import com.google.inject.Inject;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.letva.toink.blog.model.Post;
import org.letva.toink.blog.model.User;
import org.letva.toink.blog.service.BlogService;

import java.util.List;

/**
 * Homepage
 */
public class HomePage extends WebPage {

    private static final long serialVersionUID = 1L;

    @Inject
    private BlogService blogService;

    @SuppressWarnings("serial")
    public HomePage() {

        List<Post> posts = blogService.getNewestPosts(10);
        ListView<Post> listView = new ListView<Post>("postList", posts) {
            protected void populateItem(ListItem<Post> item) {
                // empty markup where details about the author of the post will be
                WebMarkupContainer authorDetails = new WebMarkupContainer("authorDetails");
                authorDetails.setOutputMarkupPlaceholderTag(true);
                item.add(authorDetails);

                // ajax link to show/hide details about the author
                AjaxLink authorAjaxLink = new ShowHideAuthorPanelLink("authorLink", item.getModelObject().getUser(), authorDetails);
                authorAjaxLink.add(new Label("author", item.getModelObject().getUser().getUsername()));
                item.add(authorAjaxLink);

                // empty markup where details about the post will be
                WebMarkupContainer postDetails = new WebMarkupContainer("postDetails");
                postDetails.setOutputMarkupPlaceholderTag(true);
                item.add(postDetails);

                // ajax link to show/hide details about the post
                AjaxLink postAjaxLink = new ShowHidePostPanelLink("postLink", item.getModelObject(), postDetails);
                postAjaxLink.add(new Label("title", item.getModelObject().getTitle()));
                item.add(postAjaxLink);
            }
        };
        add(listView);

    }

    public class ShowHideAuthorPanelLink extends AjaxLink {
        private WebMarkupContainer authorDetails;
        private Panel authorDetailsPanel;
        private boolean detailsVisible = false;
        private User user;

        public ShowHideAuthorPanelLink(String id, User user, WebMarkupContainer authorDetails) {
            super(id);
            this.user = user;
            this.authorDetails = authorDetails;
            this.authorDetails.setOutputMarkupPlaceholderTag(true);
        }
        @Override
        public void onClick(AjaxRequestTarget target) {
            if (!detailsVisible) {
                authorDetailsPanel = new UserDetailsPanel("authorDetails", user, this);
                authorDetailsPanel.setOutputMarkupPlaceholderTag(true);
                authorDetails.replaceWith(authorDetailsPanel);
                target.addComponent(authorDetailsPanel);
                detailsVisible = true;
            } else {
                authorDetailsPanel.replaceWith(authorDetails);
                target.addComponent(authorDetails);
                detailsVisible = false;
            }
        }
    }

    public class ShowHidePostPanelLink extends AjaxLink {
        private WebMarkupContainer postDetails;
        private Panel postDetailsPanel;
        private boolean detailsVisible = false;
        private Post post;

        public ShowHidePostPanelLink(String id, Post post, WebMarkupContainer postDetails) {
            super(id);
            this.post = post;
            this.postDetails = postDetails;
            this.postDetails.setOutputMarkupPlaceholderTag(true);
        }
        @Override
        public void onClick(AjaxRequestTarget target) {
            if (!detailsVisible) {
                postDetailsPanel = new PostDetailsPanel("postDetails", post);
                postDetailsPanel.setOutputMarkupPlaceholderTag(true);
                postDetails.replaceWith(postDetailsPanel);
                target.addComponent(postDetailsPanel);
                detailsVisible = true;
            } else {
                postDetailsPanel.replaceWith(postDetails);
                target.addComponent(postDetails);
                detailsVisible = false;
            }
        }
    }
}
